//
//  ChildHoodService.m
//  ChildHoodStemp
//
//  Created by xuzuotao on 13-8-24.
//
//

#import "ChildHoodService.h"
#import "CFolderMgr.h"
#import "CAccountModel.h"


static ChildHoodService *_service;
@implementation ChildHoodService

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 此处的代码可以保证在多线程中，只会被执行一次！
        _service = [super allocWithZone:zone];
    });
    
    return _service;

    
}

+ (instancetype)defaultService
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _service = [[ChildHoodService alloc] init];
    });
    
    return _service;

}

- (id)copyWithZone:(NSZone *)zone
{
    return _service;
}

- (NSString *)filePath
{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return paths.lastObject;
}

- (NSString *)personalFilePath
{
    CAccountModel *curAcc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    return [[self filePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/Personal",IMIP, M_STOREVALUE(curAcc.userName)]];
}

- (NSString *)groupFilePath
{
    CAccountModel *curAcc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    return [[self filePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/GroupShare",IMIP, M_STOREVALUE(curAcc.userName)]];
}

- (NSString *)personalUploadFilePath
{
    return [[self personalFilePath] stringByAppendingPathComponent:@"upload"];
}

- (NSString *)personalDownloadFilePath
{
    return [[self personalFilePath] stringByAppendingPathComponent:@"download"];
}

- (NSString *)groupUploadFilePath
{
    return [[self groupFilePath] stringByAppendingPathComponent:@"upload"];
}

- (NSString *)groupDownloadFilePath
{
    return [[self groupFilePath] stringByAppendingPathComponent:@"download"];
}
- (NSString *)personalUploadSoundPathWithUserId:(NSString *)userId
{

    NSString *path = [[self personalUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"sound"]];
   BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }


    return path;
}
-(NSString *)personalUploadVideoPathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"video"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return path;
}
-(NSString *)personalDownloadVideoPathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"video"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return path;
}
- (NSString *)personalDownloadSoundPathWithUserId:(NSString *)userId
{
    
    NSString *path = [[self personalDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"sound"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    
    return path;
}

- (NSString *)groupUploadSoundPathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"sound"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    
    return path;
    
}
-(NSString *)groupuploadVideoPathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"video"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    
    return path;

}
-(NSString *)groupDownloadVideoPathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"video"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    
    return path;
}
- (NSString *)groupDownloadSoundPathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"sound"]];
   BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    
    return path;

}
- (NSString *)personalUploadImagePathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"image"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }

    return path;

}

- (NSString *)personalDownloadImagePathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"image"]];
   BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}



- (NSString *)groupUploadImagePathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"image"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}
- (NSString *)groupUploadVideoPathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"video"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}
- (NSString *)groupDownloadImagePathWithGroupId:(NSString *)groupId
{
    NSString *path = [[self groupDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",groupId,@"image"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}


- (NSString *)personalDownLoadFilePathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"file"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}


- (NSString *)personalUpLoadFilePathWithUserId:(NSString *)userId
{
    NSString *path = [[self personalUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",userId,@"file"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }

    return path;

}

- (NSString *)groupDownLoadFilePathWithGroupId:(NSString *)GroupId
{
    NSString *path = [[self groupDownloadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",GroupId,@"file"]];
    BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;
    
}


- (NSString *)groupUpLoadFilePathWithGroupId:(NSString *)GroupId
{
    NSString *path = [[self groupUploadFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",GroupId,@"file"]];
   BOOL isdir = NO;
    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
    if (!isdir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return path;

}

- (void)saveGroupUpLoadFileWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupUpLoadFilePathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (content.length == contData.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }

}

- (void)saveGroupDownLoadFileWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupDownLoadFilePathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalUpLoadFileWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalUpLoadFilePathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalDownLoadFileWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalDownLoadFilePathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}


- (void)saveGroupUpLoadSoundWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupUploadSoundPathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)saveGroupDownLoadSoundWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupDownloadSoundPathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalUpLoadSoundWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalUploadSoundPathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalDownLoadSoundWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalDownloadSoundPathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}


- (void)saveGroupUpLoadImageWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupUploadImagePathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)saveGroupUpLoadVideoWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupUploadVideoPathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}
- (void)saveGroupDownLoadImageWithFileName:(NSString *)fileName content:(NSData *)content groupId:(NSString *)GroupId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self groupDownloadImagePathWithGroupId:GroupId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalUpLoadImageWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalUploadImagePathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}

- (void)savePersonalDownLoadImageWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalDownloadImagePathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
    
}
-(void)savePersonalUpLoadVideoWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalUploadVideoPathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
}
-(void)savePersonalDownLoadVideoWithFileName:(NSString *)fileName content:(NSData *)content userId:(NSString *)userId
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[self personalDownloadVideoPathWithUserId:userId] stringByAppendingPathComponent:fileName];
    BOOL isExist = [fileManager fileExistsAtPath:path];
    if (isExist) {
        NSData *contData = [fileManager contentsAtPath:path];
        if (contData.length == content.length) {
            return;
        }
    }
    if (content.length > 0) {
        //        //未做处理
        //        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
        //        }
        //
        [fileManager createFileAtPath:path contents:content attributes:nil];
        
    }
}

@end